<template>
  <div style="width: 30%;margin-left: 500px">
    <h3 style="font-size: 40px">登录页</h3>
    <h5 style="font-size: 40px;">{{ message }}</h5>
    <el-form ref="form" :model="form" label-width="50px">
      <el-form-item label="账号">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      message: ""
    };
  },
  methods: {
    onSubmit() {
      console.log(JSON.stringify(this.form) + "前端传递的数据");
      // 发送请求到后台
      axios
        .post("http://localhost:8002/user/login", this.form)
        .then(response => {
          console.log(JSON.stringify(response.data.data)+"==============");
          if (response.data.data !==null) {
            // 登录成功
            this.$message.success("登录成功");
            this.$router.push("/home?uid=" + response.data.data.uid);
          } else {
            this.$message.error("登陆失败");
          }
        })
        .catch(error => {
          console.log(JSON.stringify(error)+"失败");
        });
    }
  }
};
</script>

<style scoped></style>
